/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "Application.h"
#include <lib2d/gui/ScrollBar.h>
#include <lib2d/gui/ListBox.h>
#include <lib2d/gui/CheckBox.h>
#include <lib2d/gui/EditBox.h>
#include <lib2d/gui/Dialog.h>

namespace liba
{
namespace lib2d
{


Application::Application(const std::string & caption)
:	ApplicationWindowed( caption )
{
	renderer = lib2d::hard::Manager::get_hardwares().front();
	MoveWindow( get_window()->get_wnd(), 0, 0, 640, 480, false );
	renderer->set_mode( get_window(), 800, 600, false );
	//		throw Gluck(__FUNCTION__, "Could not create desired videomode");

	image = new objects::ImageSimple( "hren" );
	text = new objects::TextSprite( "comic.font" );
	text->change_text( L"HREN VAM" );

	frame_set = new objects::FrameSet( "button.fst" );

	main_gui = new gui::Desktop( Rect( 0, 0, 640, 480 ), Rect( 0, 0, 640, 480 ) );
	new gui::Button( main_gui, Rect( 0, 0, 50, 50 ) );
	new gui::Button( main_gui, Rect( 100, 100, 50, 50 ) );
	new gui::ScrollBar( main_gui );
	new gui::CheckBox( main_gui, Rect( 300, 100, 50, 50 ) );
	new gui::EditBox( main_gui );
	new gui::Dialog( main_gui );
	gui::ListBox * lb = new gui::ListBox( main_gui );
	for( int i = 0; i < 10; ++i )
		new gui::listbox::Item( lb );

	anim = new objects::Animation( "spider" );
	anim->start( Atom("Main" ) );
}

Application::~Application()
{
	if( renderer )
		renderer->stop_mode();
	renderer = 0;
}

bool Application::life_cycle( double delta_time )
{
	input::Manager::life_cycle();
	objects::ImageSimple::load_reps();
	objects::Sequence::load_reps();

	anim->life_cycle( delta_time );
	static int iii = 0;
	if( anim->is_finished() )
	{
		++iii;
		if( iii % 4 )
			anim->start( Atom("Main" ) );
		else
			anim->start( Atom("PrepUp" ) );
	}

	main_gui->life_cycle( delta_time );

	renderer->start_scene( Rect( 0, 0, 800, 600) );
	renderer->fill( Rect( 0, 0, 640, 480 ), ColorPacked( 200, 200, 200 ) );
	main_gui->get_view()->render( renderer );
	text->render( renderer, Vector2( 300, 300 ) );
	frame_set->render( renderer, Vector2( 150, 150 ), 8 );
	anim->render( renderer );
//	image->blit( renderer, Vector2( 0, 0 ) );
	renderer->flip();

	return ApplicationWindowed::life_cycle( delta_time );
}

int Application::window_proc( unsigned msg, unsigned wParam, unsigned lParam )
{
	switch (msg) 
	{
	case WM_SIZE:
		{
			RECT rect;
			GetClientRect( get_window()->get_wnd(), &rect );
			if( main_gui )
			{
				main_gui->on_mode_change( Rect( rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top ) );
				main_gui->change_windowrect( Rect( rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top ) );
			}
		}
	}
	return ApplicationWindowed::window_proc( msg, wParam, lParam );
}

} // namespace lib2d
} // namespace liba

